package com.yhao.floatwindow;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;
import org.opencv.features2d.FeatureDetector;

class FloatPhone extends FloatView {
    private final Context mContext;
    private final WindowManager.LayoutParams mLayoutParams;
    private View mView;
    private final WindowManager mWindowManager;
    private int mX;
    private int mY;

    FloatPhone(Context context) {
        this.mContext = context;
        this.mWindowManager = (WindowManager) context.getSystemService("window");
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        this.mLayoutParams = layoutParams;
        layoutParams.format = 1;
        layoutParams.flags = 296;
        layoutParams.type = FeatureDetector.PYRAMID_DENSE;
        layoutParams.windowAnimations = 0;
    }

    @Override // com.yhao.floatwindow.FloatView
    public void setSize(int i, int i2) {
        this.mLayoutParams.width = i;
        this.mLayoutParams.height = i2;
    }

    @Override // com.yhao.floatwindow.FloatView
    public void setView(View view) {
        this.mView = view;
    }

    @Override // com.yhao.floatwindow.FloatView
    public void setGravity(int i, int i2, int i3) {
        this.mLayoutParams.gravity = i;
        WindowManager.LayoutParams layoutParams = this.mLayoutParams;
        this.mX = i2;
        layoutParams.x = i2;
        WindowManager.LayoutParams layoutParams2 = this.mLayoutParams;
        this.mY = i3;
        layoutParams2.y = i3;
    }

    @Override // com.yhao.floatwindow.FloatView
    public void init() {
        if (Build.VERSION.SDK_INT >= 25) {
            req();
        } else if (!Miui.rom()) {
            try {
                this.mLayoutParams.type = FeatureDetector.PYRAMID_ORB;
                this.mWindowManager.addView(this.mView, this.mLayoutParams);
            } catch (Exception unused) {
                this.mWindowManager.removeView(this.mView);
                req();
            }
        } else if (Build.VERSION.SDK_INT >= 23) {
            req();
        } else {
            this.mLayoutParams.type = 2002;
            Miui.req(this.mContext, new PermissionListener() {
                /* class com.yhao.floatwindow.FloatPhone.AnonymousClass1 */

                @Override // com.yhao.floatwindow.PermissionListener
                public void onFail() {
                }

                @Override // com.yhao.floatwindow.PermissionListener
                public void onSuccess() {
                    FloatPhone.this.mWindowManager.addView(FloatPhone.this.mView, FloatPhone.this.mLayoutParams);
                }
            });
        }
    }

    private void req() {
        try {
            if (Build.VERSION.SDK_INT >= 26) {
                this.mLayoutParams.type = 2038;
            } else {
                this.mLayoutParams.type = 2002;
            }
            this.mWindowManager.addView(this.mView, this.mLayoutParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override // com.yhao.floatwindow.FloatView
    public void dismiss() {
        this.mWindowManager.removeView(this.mView);
    }

    @Override // com.yhao.floatwindow.FloatView
    public void updateXY(int i, int i2) {
        WindowManager.LayoutParams layoutParams = this.mLayoutParams;
        this.mX = i;
        layoutParams.x = i;
        WindowManager.LayoutParams layoutParams2 = this.mLayoutParams;
        this.mY = i2;
        layoutParams2.y = i2;
        this.mWindowManager.updateViewLayout(this.mView, this.mLayoutParams);
    }

    /* access modifiers changed from: package-private */
    @Override // com.yhao.floatwindow.FloatView
    public void updateSize(int i, int i2) {
        this.mLayoutParams.width = i;
        this.mLayoutParams.height = i2;
        this.mWindowManager.updateViewLayout(this.mView, this.mLayoutParams);
    }

    /* access modifiers changed from: package-private */
    @Override // com.yhao.floatwindow.FloatView
    public void updateX(int i) {
        WindowManager.LayoutParams layoutParams = this.mLayoutParams;
        this.mX = i;
        layoutParams.x = i;
        this.mWindowManager.updateViewLayout(this.mView, this.mLayoutParams);
    }

    /* access modifiers changed from: package-private */
    @Override // com.yhao.floatwindow.FloatView
    public void updateY(int i) {
        WindowManager.LayoutParams layoutParams = this.mLayoutParams;
        this.mY = i;
        layoutParams.y = i;
        this.mWindowManager.updateViewLayout(this.mView, this.mLayoutParams);
    }

    /* access modifiers changed from: package-private */
    @Override // com.yhao.floatwindow.FloatView
    public int getX() {
        return this.mX;
    }

    /* access modifiers changed from: package-private */
    @Override // com.yhao.floatwindow.FloatView
    public int getY() {
        return this.mY;
    }
}
